package com.seezoon.dao.autoconfigure;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.seezoon.dao.framework.authority.DataAuthorityLoader;
import com.seezoon.dao.framework.dto.UserInfo;
import org.apache.ibatis.reflection.MetaObject;

import java.time.Instant;
import java.util.Date;

/**
 * @author sharonlee
 * @since 2022/7/15 13:40
 */
public class MyBatisPlusAuditHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        UserInfo user = DataAuthorityLoader.getInstance().getUser();
        if (user != null) {
            setFieldValByName("createBy", user.getUserId(), metaObject);
        }
        setFieldValByName("createTime", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        UserInfo user = DataAuthorityLoader.getInstance().getUser();
        if (user != null) {
            setFieldValByName("updateBy", user.getUserId(), metaObject);
        }
        setFieldValByName("updateTime", new Date(), metaObject);
    }
}
